//
//  main.m
//  05_Work
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 yh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Province.h"
#import "City.h"
#import "District.h"

int main(int argc, const char * argv[]) {
// 一、
// 1. 创建1个包含5个字符串元素的数组；
// 2. 用for循环倒序输出所有元素；
// 3. 用for...in完成所有字符串元素的拼接(生成一个新的字符串)，并输出拼接好的字符串。
    
    NSArray *array = @[@"zhaohao", @"chenchen", @"huaidan", @"yaoyao", @"xiaojiahuo"];
    
    for (NSInteger i = array.count - 1; i >= 0; i--) {
        NSLog(@"%@", array[i]);
    }
    
    NSString *arrStr = [NSString string];
    for (NSString *str in array) {
        arrStr = [arrStr stringByAppendingString:str];
    }
    NSLog(@"%@", arrStr);
    
// 二、
// 1. 创建1个包含5个person对象的数组(姓名用英文)；
// 2. 用for循环找到年龄最大的Person对象；
// 3. 用for...in遍历数组，把大于某个年龄的Person对象的姓名后面拼接上字符串“-brother”。
    
    Person *per1 = [[Person alloc] init];
    per1.name = @"zhaohao";
    per1.age = 20;
    Person *per2 = [[Person alloc] init];
    per2.name = @"xiaotan";
    per2.age = 24;
    Person *per3 = [[Person alloc] init];
    per3.name = @"amei";
    per3.age = 27;
    Person *per4 = [[Person alloc] init];
    per4.name = @"huaidan";
    per4.age = 22;
    Person *per5 = [[Person alloc] init];
    per5.name = @"weiwei";
    per5.age = 26;
    NSArray *perArr = @[per1, per2, per3, per4, per5];
    NSInteger max = 0;
    NSString *maxName = [NSString string];
    for (NSInteger i = 0; i < perArr.count; i++) {
        if ([perArr[i] age] > max) {
            max = [perArr[i] age];
            maxName = [perArr[i] name];
        }
    }
    NSLog(@"%@是年龄最大的 今年%ld岁", maxName, max);
    
    NSInteger maxAge = 0;
    NSString *nameStr = [NSString string];
    for (Person *per in perArr) {
        if (per.age > maxAge) {
            maxAge = per.age;
            nameStr = per.name;
        }
    }
    nameStr = [nameStr stringByAppendingString:@"- brother"];
    NSLog(@"%@", nameStr);
    
// 三、
// 1. 使用第二题目中生成的数组；
// 2. 用sortUsingSelector: 分别按年龄，姓名，性别进行排序，输出排序结果； 是可变数组的一个比较的方法
// 3. 使用NSSortDescriptor，按姓名进行排序。
    NSMutableArray *newPerArr = [NSMutableArray arrayWithArray:perArr];
    [newPerArr sortUsingSelector:@selector(compareName:)];
    NSLog(@"按照姓名排序排序: %@", newPerArr);
    [newPerArr sortUsingSelector:@selector(compareAge:)];
    NSLog(@"按照年龄排序%@", newPerArr);
    
//    // 1. 将perArr中的age放到一个数组ageArr中
//    NSMutableArray *ageArr = [NSMutableArray array];
//    NSArray *ageArray = [NSArray array];
//    for (NSInteger i = 0; i < perArr.count; i++) {
//        [ageArr addObject:[NSString stringWithFormat:@"%ld", [perArr[i] age]]];
//    }
//    // 2. 将存放年龄的数组进行排序
//    ageArray = ageArr;
//    NSArray *newArray = [ageArray sortedArrayUsingSelector:@selector(compare:)];
//    // 3. 循环进行比较 将数组按照年龄的顺序输出
//    NSMutableArray *newPerArray = [NSMutableArray array];
//    for (NSInteger i = 0; i < newArray.count; i++) {
//        for (NSInteger j = 0; j < newArray.count; j++) {
//            if ([newArray[i] integerValue] == [perArr[j] age]) {
//                [newPerArray addObject:perArr[j]];
//            }
//        }
//    }
//    NSLog(@"%@", newPerArray);

    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *newPerArray1 = [perArr sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"%@", newPerArray1);
    
// 四、
// 处理省市区文件中的area.txt
// 将省市区进行分割处理, 省里包含市, 市里包含区
// 1. 先用数组字典表示

    NSString *path = @"/Users/dllo/Desktop/Homework-05/area.txt";
    // 通过路径读取文件中的字符 将.txt转化为字符串
    NSString *areaStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 按照回车截取字符串 将截取后的字符串放到数组中
    NSArray *areaArr = [areaStr componentsSeparatedByString:@"\n"];
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *area in areaArr) {
        if (![area hasPrefix:@" "]) {
           // 创建一个 省的字典其中包括两个 键值对 一个是省名 另一个是所在该省的市的数组
           NSMutableArray *cityArray = [NSMutableArray array];
           NSMutableDictionary *proDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:area, @"province", cityArray, @"city", nil];
            // 将省的字典存入最外层的数组中
            [proArr addObject:proDic];
        } else if (![area hasPrefix:@"    "]) {
            // 判断出来的是市名 创建一个市的字典 和区的数组 市的字典里面存放的是市名 和所在该市的区的数组
            NSMutableArray *quArray = [NSMutableArray array];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:area, @"city",quArray, @"qu", nil];
            // 创建省的字典 在第一个判断里 将省的字典存放到了省的数组 获取数组中最后一个元素 即为在该判断里市所在的省
            NSMutableDictionary *proDic = [proArr lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"city"];
            [cityArray addObject:cityDic];
        } else {
            NSMutableDictionary *proDic = proArr.lastObject;
            NSMutableArray *cityArr = [proDic objectForKey:@"city"];
            NSMutableDictionary *cityDic = cityArr.lastObject;
            NSMutableArray *areaArr = [cityDic objectForKey:@"qu"];
            [areaArr addObject:area];
        }
    }
    NSLog(@"%@", proArr);
    
// 2. 将数组字典的处理方式转换成数组模型(Class)
//    NSMutableArray *allPlaceArr = [NSMutableArray array];
//    for (NSString *area in areaArr) {
//        if (![area hasPrefix:@" "]) {
//            Province *pro = [[Province alloc] init];
//            pro.proName = area;
//            pro.citysArr = [NSMutableArray array];
//            [allPlaceArr addObject:pro];
//        } else if (![area hasPrefix:@"    "]) {
//            City *city = [[City alloc] init];
//            city.cityName = area;
//            city.districtsArr = [NSMutableArray array];
//            [[allPlaceArr.lastObject citysArr] addObject:city];
//        } else {
//            District *district = [[District alloc] init];
//            district.districtName = area;
//            [[[[[allPlaceArr lastObject] citysArr]lastObject] districtsArr] addObject:area];
//        }
//    }
//    NSLog(@"%@", allPlaceArr);
    
// 五、拓展
// 上述作业做完, 处理Location1.txt
// 要求同第四题。自己寻找规律
    
    NSString *str = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/05_Work/05_Work/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [str componentsSeparatedByString:@","];
    // 对数组进行排序 排序后就和之前的省市区的做法一样
    arr = [arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        // 将obj1 obj2 已"|" 截取
        NSArray *arr1 = [obj1 componentsSeparatedByString:@"|"];
        NSArray *arr2 = [obj2 componentsSeparatedByString:@"|"];
        // 分别取出数组第二个元素 并转化为NSInteger
        NSInteger num1 = [arr1[1] integerValue];
        NSInteger num2 = [arr2[1] integerValue];
        if (num1 > num2) {
            // 降序
            return NSOrderedDescending;
        } else if (num1 < num2) {
            return NSOrderedAscending;
        } else {
            return NSOrderedSame;
        }
    }];
    NSMutableArray *allAreaArr = [NSMutableArray array];
    // 进行遍历
    for (NSString *area in arr) {
        if ([area hasSuffix:@"0000"]) {
            NSMutableArray *cityArr = [NSMutableArray array];
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:area, @"省", cityArr, @"市", nil];
            [allAreaArr addObject:proDic];
        } else if ([area hasSuffix:@"00"]) {
            NSMutableArray *districtArr = [NSMutableArray array];
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:area, @"市", districtArr, @"区", nil];
            NSMutableDictionary *proDic = allAreaArr.lastObject;
            NSMutableArray *cityArr = [proDic objectForKey:@"市"];
            [cityArr addObject:cityDic];
        } else {
            NSMutableDictionary *proDic = allAreaArr.lastObject;
            NSMutableArray *cityArr = [proDic objectForKey:@"市"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *districtArr = [cityDic objectForKey:@"区"];
            [districtArr addObject:area];
        }
    }
        NSLog(@"%@", allAreaArr);
    
    NSMutableArray *allLocationArr = [NSMutableArray array];
    for (NSString *area in arr) {
        if ([area hasSuffix:@"0000"]) {
            Province *pro = [[Province alloc] init];
            pro.proName = area;
            pro.citysArr = [NSMutableArray array];
            [allLocationArr addObject:pro];
        } else if ([area hasSuffix:@"00"]) {
            City *city = [[City alloc] init];
            city.cityName = area;
            city.districtsArr = [NSMutableArray array];
            [[[allLocationArr lastObject] citysArr] addObject:city];
        } else {
            [[[[[allLocationArr lastObject] citysArr] lastObject] districtsArr] addObject:area];
        }
    }
    NSLog(@"lll: %@", allLocationArr);
    
    
    
    
    
    
    
    
// 六、如果都做完
// 额外找我要作业, 练习有的是, 就怕你不练
    
    
    
    
    
    return 0;
}
